<template>
  <div class="notice">
    <div class="notice-header">
      <!-- 列表查找 -->
      <!-- <orderStatistics :myInfo ='myInfo'  @getData='getTableData'/> -->
      <el-button type="primary" icon="el-icon-plus" @click='addNotice' class='addNotice'>发布公告</el-button>
      <div class="search-wrap header_search">
        <table-search
          class='clearfloat'
          ref="table-search"
          :selectHead="selectHead"
          @searchMethods="searchMethods">
        </table-search>
      </div>
    </div>


    <el-table
      ref="tableRef"
      :data="tableData"
      border
      style="width: 100%"
      v-loading="loading"
      @sort-change='tableSortChange'
      height="88%">
      <el-table-column show-overflow-tooltip label="ID" width="60" align="center">
        <template slot-scope="scope">{{(scope.$index+1)+(currentPage-1)*tableSize }}</template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="公告标题" align="left" prop='title' sortable='custom' min-width="200"></el-table-column>
      <el-table-column show-overflow-tooltip label="公告摘要" align="left" prop='abstract' sortable='custom' min-width="200"></el-table-column>
      <el-table-column show-overflow-tooltip label="发布人" align="left" prop='publisher' sortable='custom' min-width="200"></el-table-column>
      <el-table-column show-overflow-tooltip label="公告内容" align="left" prop='news' sortable='custom' min-width="200"></el-table-column>
      <el-table-column show-overflow-tooltip label="接收角色" align="left" prop='role_id' sortable='custom' min-width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.role_id | getRoleName }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="发送时间" align="left" prop='created_at' sortable='custom' min-width="200"></el-table-column>

      <!-- <el-table-column
        fixed="right"
        align="center"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.status != 0" @click="review(scope.row)" type="warning" size="small">审核</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination @paginationChange="paginateChange" :total="total"></pagination>
    <addNotice ref='addNotice'  @refresh='getTableData'> </addNotice>
  </div>
</template>

<script>
import orderStatistics from "~/components/orderStatistics"
import { getMyInfo } from "~/api/user"
import Pagination from '~/components/pagination'
import tableSearch from '~/components/tableSearch'

import addNotice from './components/notice-add'

export default {
  data () {
    return {
      tableData: [],
      loading: false,
      pageSize: 25, //一页可以几条
      currentPage: 1, //目前在第几页
      tableSize: 25, //一页几条
      total: 0,
      paramsByNotice: {},
      myInfo: {
        agent_grades_appeal : 0
      },
      selectHead: [
        {
          label: '公告标题',
          props: 'title'
        },
        {
          label: '公告内容',
          props: 'news'
        },
      ],
      search_params: {}
    }
  },
  components: {
    Pagination,
    orderStatistics,
    tableSearch,
    addNotice
  },
  created () {
    this.getTableData()
  },
  methods: {
    // async getMyinfo (params) {
    //   let myInfo = await getMyInfo(this.$axios, params);
    //   if (myInfo.data.status == 'success') {
    //     this.$store.commit("setRoleInfo",myInfo.data.data)
    //     this.$store.commit("setUser",myInfo.data.data)
    //     this.myInfo = myInfo.data.data
    //     this.roleID = this.myInfo.role_id
    //   }
    // },
    addNotice() {
      this.$refs['addNotice'].show()
    },
    getTableData (args) {
      let params = {
        perPage: this.pageSize,
        page: this.currentPage,
      }
      params = {...params, ...this.paramsByNotice, ...this.search_params}
      if (args) {
        params = {...params, ...args}
      }
      this.$axios.get('api/news', {
        params: params
      }).then(res => {
        if (res.data.code == 200) {
          this.loading = false
          this.tableData = res.data.data
          this.pageSize = res.data.page.per_page
          this.total = res.data.page.total
          // this.getMyinfo()
        } else {
          this.$message.error('服务器繁忙')
        }
      })
    },

    searchMethods(params, no_search) {
      this.sort_params = {}
      this.currentPage = 1
      this.pageSize = 25
      this.search_params = params
      if (params.time) {
        this.search_params.start_time = params.time[0]
        this.search_params.end_time = params.time[1]
        delete this.search_params.time
      }
      if (!no_search) {
          this.getTableData()
      }
    },
    paginateChange(params) {
      this.pageSize = params.perPage
      this.currentPage = params.page
      this.getTableData()
    },
    tableSortChange (column) {
      if (column.order === 'descending') {
        this.paramsByNotice.sort_str = column.prop
        this.paramsByNotice.sort_order = 'desc'
      } else if (column.order === 'ascending'){
        this.paramsByNotice.sort_str = column.prop
        this.paramsByNotice.sort_order = 'asc'
      } else {
        delete this.paramsByNotice.sort_str
        delete this.paramsByNotice.sort_order
      }
      this.loading = true
      this.currentPage = 1
      this.getTableData()
    },

  }
}
</script>

<style lang='less' type='text/css'>
  .notice {
    height: 100%;
    .notice-header {
      margin-bottom: 20px;
      .header_search {
        float: right;
      }
    }
    .el-table {
      width: 99.9% !important;
    }
  }
</style>
